package cn.bytecollege.controller;

import cn.bytecollege.entity.Admin;
import cn.bytecollege.service.AdminService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 检查管理员是否在数据库中存在的控制器
 * @author qy
 */
@WebServlet(name = "AdminCheckNameController", value = "/adminUserCheckUsername")
public class AdminCheckNameController extends HttpServlet {
    AdminService adminService = new AdminService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset = utf-8");
        String msg = null;
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username!=null){
            //没有输入用户名提示要输入
            if(username.equals("")){
                msg = "请输入用户名";
                response.getWriter().write(msg);
                return;
            }
            Admin admin = adminService.findByAdminName(username);
            //已经在数据库中添加了，需要换新用户名
            if (admin!=null){
                //该用户已经被注册
                msg = "该用户名已经存在";
                response.getWriter().write(msg);
                return;
            }else if (admin == null){
                msg = "该用户名可以使用";
                response.getWriter().write(msg);
                return;
            }
        }
        //没有输入密码提示要输入
        if(password.equals("")){
            msg = "请输入密码";
            response.getWriter().write(msg);
            return;
        }
    }
}
